﻿using LiveCharts;
using LiveCharts.Wpf;
using MonitorPlatform.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MonitorPlatform.ViewModel
{
    public class FirstPageViewModel : NotifyBase
    {
        private int _instrumentValue = 820;

        public int InstrumentValue
        {
            get { return _instrumentValue; }
            set { _instrumentValue = value; this.DoNotify(); }
        }

        private int _itemCount;

        public int ItemCount
        {
            get { return _itemCount; }
            set { _itemCount = value; this.DoNotify(); }
        }

        private double _currentLevel = 2.1;

        public double CurrentLevel
        {
            get { return _currentLevel; }
            set { _currentLevel = value; this.DoNotify(); }
        }

        private SeriesCollection _llSeries;

        public SeriesCollection llSeries
        {
            get { return _llSeries; }
            set { _llSeries = value; this.DoNotify(); }
        }

        List<string> _lineXLabels = new List<string>();
        public List<string> LineXLabels
        {
            get
            {
                return _lineXLabels;
            }

            set
            {
                _lineXLabels = value;
                this.DoNotify();
            }
        }





        Random random = new Random();
        bool taskSwitch = true;
        List<Task> taskList = new List<Task>();
        public FirstPageViewModel()
        {
            this.RefreshInstrumentValue();
            InstrumentValue = random.Next(0, 2000);
            llSeries = new SeriesCollection()
            {
                new LineSeries
                {
                    Title="液位",
                    DataLabels=true,
                    Values = new ChartValues<double> { 2.3, 1.5, 4.7, 6.4,3.0,3.2,2.1 },
                    
                    //Fill=new SolidColorBrush(Colors.LightGreen),
                },
            };

            List<string> _dates = new List<string>();
            _dates = GetCurrentMonthDates();
            for (int i = 6; i >= 0; i--)
            {
                LineXLabels.Add(_dates[i]);
            }
            

            //this.InitCourseSeries();
        }

        List<string> GetCurrentMonthDates()
        {
            List<string> dates = new List<string>();
            DateTime dt = DateTime.Now;
            int year = dt.Year;
            int mouth = dt.Month;
            int days = DateTime.DaysInMonth(year, mouth);
            //本月第一天时间      
            //DateTime dt_First = dt.AddDays(1 - (dt.Day));
            dates.Add(String.Format("{0:d}", dt.Date));

            for (int i = 1; i < 7; i++)
            {
                DateTime temp = dt.AddDays(i*(-1));
                dates.Add(String.Format("{0:d}", temp.Date));
            }
            return dates;
        }


        private void RefreshInstrumentValue()
        {
            var task = Task.Factory.StartNew(new Action(async () =>
            {
                while (taskSwitch)
                {
                    InstrumentValue = random.Next(Math.Max(this.InstrumentValue - 10, 0), Math.Min(this.InstrumentValue + 10, 2000));

                    await Task.Delay(1000);
                }
            }));
            taskList.Add(task);
        }

        public void Dispose()
        {
            try
            {
                taskSwitch = false;
                Task.WaitAll(this.taskList.ToArray());
            }
            catch { }

        }
    }
}
